package com.cuckoo.cuckoo_app2.ui.profile;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.cuckoo.cuckoo_app2.MyApplication;
import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.UserApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.UserApi;
import com.cuckoo.cuckoo_app2.business.Imp.UserBusinessImp;
import com.cuckoo.cuckoo_app2.business.UserBusiness;
import com.cuckoo.cuckoo_app2.dao.User;
import com.cuckoo.cuckoo_app2.utils.DialogUtils;
import com.cuckoo.cuckoo_app2.utils.RouteUtils;
import com.google.gson.JsonObject;


public class ProfileFragment extends Fragment {

    private ViewHolder holder;
    private UserApi userApi;
    private Context context;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TEST", "ProfileFragment.onCreate()");

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("TEST", "ProfileFragment.onCreateView()");
        return inflater.inflate(R.layout.fragment_profile, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i("TEST", "ProfileFragment.onViewCreated()");
        this.context = getContext();
        this.userApi = new UserApiImp();
        this.holder = new ViewHolder(view);

        this.bindClickeButtom();
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i("TEST", "ProfileFragment.onStart()");
        User user = MyApplication.getInstances().getUser();

        // 判断是否VIP
        if (user.getIsVip().equals(1)) {
            holder.vipView.setVisibility(View.VISIBLE);
        } else {
            holder.vipView.setVisibility(View.GONE);
        }
        holder.userName.setText(user.getName());
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("TEST", "ProfileFragment.onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i("TEST", "ProfileFragment.onPause()");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i("TEST", "ProfileFragment.onStop()");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("TEST", "ProfileFragment.onDestroyView()");
    }

    public void bindClickeButtom() {
        // 退出登录
        holder.mOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.showConfirmDialog(getContext(), "退出提示", "确认要退出当前登录吗？",
                        (dialog, which) -> {
                            // 处理确定按钮点击
                            userApi.performLogoutAsync(new BaseCallbac<JsonObject>() {
                                @Override
                                public void onSuccess(JsonObject data) {
                                    if (!data.get("success").getAsBoolean()) {
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                DialogUtils.showToast(getContext(), data.get("message").getAsString());
                                            }
                                        });
                                        return;
                                    }
                                    // 删除数据库
                                    UserBusiness userBusiness = new UserBusinessImp();
                                    userBusiness.logout();
                                    // 删除缓存
                                    MyApplication.getInstances().clearUser();
                                    // 跳转登录页面
                                    RouteUtils.toLoginActivity(getContext());
                                }

                                @Override
                                public void onFailure(String errorMessage) {

                                }
                            });


                        });
            }
        });
        // 修改密码
        holder.mPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtils.toChangePasswordActivity(context);
            }
        });
        // 修改手机
        holder.mMobilePhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtils.toChangePhoneActivity(context);
            }
        });
        // 购买会员
        holder.mPurchaseMembership.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtils.toVipMembershipAcitvity(context);
            }
        });
        // 系统反馈
        holder.mSystemFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtils.toSystemFeedbackActivity(context);
            }
        });
        // 财务管理
        holder.mFinancialManagement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtils.toWalletActivity(context);
            }
        });
        // 我的邀请
        holder.mMyInvitation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtils.toMyInvitationActivity(context);
            }
        });
    }


    public class ViewHolder {
        LinearLayout mOut;
        LinearLayout mPassword;
        LinearLayout mMobilePhone;
        LinearLayout mPurchaseMembership;
        LinearLayout mSystemFeedback;
        LinearLayout mFinancialManagement;
        LinearLayout mMyInvitation;
        TextView userName;
        ImageView vipView;

        public ViewHolder(View view) {
            mMyInvitation = view.findViewById(R.id.m_my_invitation);
            mFinancialManagement = view.findViewById(R.id.m_financial_management);
            mSystemFeedback = view.findViewById(R.id.m_system_feedback);
            mPurchaseMembership = view.findViewById(R.id.m_purchase_membership);
            mMobilePhone = view.findViewById(R.id.m_mobile_phone);
            mPassword = view.findViewById(R.id.m_password);
            mOut = view.findViewById(R.id.m_out);
            vipView = view.findViewById(R.id.ic_vip_m);
            userName = view.findViewById(R.id.user_name);
        }
    }

}
